<?php
//界面风格
class StyleAction extends CommonAction {
	
    public function index(){
    	$userid = session('userid');
    	
    	if(isset($userid)){
    		$themeRoot = C('THEME_ROOT');
    		$dirArray = array();
    		$key = 0;
    		if ($handle = opendir($themeRoot)) {
		        while (false !== ($file = readdir($handle))) {
		            if ($file != "." && $file != "..") {
		                if (is_dir($themeRoot."/".$file)) {
		                    $dirArray[$key]["fileName"] = $file;
		                    $dirArray[$key]["fileRoot"] = $themeRoot."/".$file;
		                    $dirArray[$key]["fileUrl"] = __ROOT__."/theme/".$file;
		                    $key++;
		                }
		            }
		        }
		    }
		    
		    $themeInfo = array();
		    $themeStyle = array();
		    foreach($dirArray as $key=>$value){
		    	$themeInfoRoot = $value["fileRoot"]."/info/info.php";
		    	if(file_exists($themeInfoRoot)){
					$themeInfo[$key] = include($themeInfoRoot);
					$themeInfo[$key]["fileUrl"] = $value["fileUrl"];
					$themeInfo[$key]["fileRoot"] = $value["fileRoot"];
					$themeStyle[$key] = $themeInfo[$key]["style"];
				}else{
					$themeInfo[$key] = "缺少介绍文件"; 
				}
		    }
		    //将theme数据加载到数据库中
		    $data = array();
		    $themeModel = M("Theme");
		    foreach($themeInfo as $key=>$value){
	    		$checkTheme = $themeModel->where("name='{$value['id']}'")->find();
	    		
	    		if(!empty($checkTheme)){
	    			$data['name'] = $checkTheme['name'];
		    		$data['fileUrl'] = $value['fileUrl'];
		    		$data['fileRoot'] = $value['fileRoot'];
		    		$data['style'] = $checkTheme['style'];
		    		$data['styleCss'] = $checkTheme['styleCss'];
		    		$themeModel->where("name='{$value['id']}'")->save($data);
		    		
		    		$themeInfo[$key]['selectStyle'] = $checkTheme['style'];
		    		$themeInfo[$key]['openStyle'] = $checkTheme['admin_ok'];
		    		
	    		}else{
		    		$data['name'] = $value['id'];
		    		$data['fileUrl'] = $value['fileUrl'];
		    		$data['fileRoot'] = $value['fileRoot'];
		    		$data['style'] = key($value['style']);
		    		$data['styleCss'] = $value['style'][$data['style']];
		    		$themeModel->add($data);
		    		
		    		$themeInfo[$key]['selectStyle'] = $value['style'][0];
	    		}
		    }
		    
		    $this->themeStyle = $themeStyle;
    		$this->themeList = $themeInfo;
    		
        	$this->display();
    	}else{
			$this->error('验证失效，重新登陆！', '__APP__/login/index');
    	}
    }
    
    public function enableStyle(){
    	$id = $this->_post('id');
    	$styleName = $this->_post('styleName');
    	$styleCss = $this->_post('styleCss');
    	
    	$themeModel = M('Theme');
    	$data = array();
    	
    	$data['admin_ok'] = 1;
    	
    	$result = $themeModel->save($data);
		
		$data['style'] = $styleName;
		$data['styleCss'] = $styleCss;
		$data['admin_ok'] = 2;
		
		$result = $userModel->where("name='{$id}'")->save($data);
		
        if(false !== $result) {
        	$this->json(array('statusCode'=>200,'message'=>'修改成功！'));
        }else{
        	$this->json(array('statusCode'=>300,'message'=>'修改错误！'));
        }
    }
    
}